<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学员ID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入学员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="orderVal">
        <el-select
          v-model="queryParams.orderVal"
          placeholder="排序"
          clearable
        >
          <el-option
            v-for="item in orderByInfo"
            :key="item.val"
            :label="item.label"
            :value="item.val"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>


    <el-table
      ref="tables"
      v-loading="loading"
      :data="unrealList"
    >
      <el-table-column label="学员ID" align="center" prop="uid" />
      <el-table-column label="学员昵称" align="center" prop="nickname" />
      <el-table-column label="答题数" align="center" prop="dones" />
      <el-table-column label="正确数" align="center" prop="rights" />
      <el-table-column label="进度" align="center" prop="doneRate" />
      <el-table-column label="正确率" align="center" prop="rightRate" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { unrealList} from "@/api/exam/unrealList";
import { isArray } from 'util'

export default {
  name: "Exam",
  data() {
    return {
      queryParams: {
        uid: null,
        orderVal: null,

        pageNo: 1,
        pageSize: 10,
      },
      startDateRange: [],
      endDateRange: [],

      orderByInfo: [
        {val: null , label: '默认'},
        {val: 'rights DESC' , label: '正确数 倒序'},
        {val: 'rights' , label: '正确数 正序'},
        {val: 'right_rate DESC' , label: '正确率 倒序'},
        {val: 'right_rate' , label: '正确率 正序'},
        {val: 'dones DESC' , label: '答题数 倒序'},
        {val: 'dones' , label: '答题数 正序'},
        {val: 'done_rate DESC' , label: '答题率 倒序'},
        {val: 'done_rate' , label: '答题率 正序'},
      ],

      categoryList: [], //科目分类
      addExamSubjectCategorySelect: null,
      currentExamSubject: null,

      showSearch: true,
      examStatusList: [],

      loading: true,
      unrealList: null,
      total: 0,

      addExamView: false,
      addTitle: '新增考试',
      addExamForm: {
        examName: null,
        status: null,
        startTime: null,
        endTime: null,
        examTheory:{
          examTime: null
        }
      },
      addExamFormTime: [],
      addExamFormRules: {
        examName: [{ required: true, message: "考试名称不能为空", trigger: "blur" }],
        startTime: [{ required: true, message: "考试时间不能为空", trigger: "blur" }],
      },

      updateExamView: false,
      updateTitle: '更新考试信息',
      updateExamForm: {
        id: null,
        examName: null,
        status: null,
        startTime: null,
        endTime: null,
        examTheory:{
          examTime: null
        }
      },
      updateExamFormTime: [],

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      let param = {...this.queryParams};
      param.pageNo --;
      unrealList(this.param).then(
        (response) => {
          this.unrealList = response.rows;
          this.total = parseInt(response.total);
          this.loading = false;
        }
      );
    },

    statusVal(status){
      for (let s of this.examStatus){
        if(s.status == status){
          return s.label;
        }
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.startDateRange = [];
      this.endDateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 详细按钮操作 */
    handleView(row) {
      this.taskNo = row.taskNo;
      this.taskView = true;
      const taskId = row.taskId;
      getTask(taskId).then((response) => {
        this.evaluate = response.data;
        this.title = "考核详情";
      });

      //人员信息
      this.getPersonlTaskList();
    },

    handleAdd(){
      this.addExamView = true;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.updateExamView = true;
      this.updateExamForm = {
        id: row.id,
        examName: row.examName,
        status: row.status,
        startTime: row.startTime,
        endTime: row.endTime,
        examTheory: {
          id: row.examTheory.id,
          examTime: row.examTheory.examTime
        }
      }
      this.updateExamFormTime = [
        this.parseTime(row.startTime, "{y}-{m}-{d} {h}:{i}:{s}"),
        this.parseTime(row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
      ];
    },
    onAddExamFormTimeChange(val){
      console.log(val);
      this.addExamForm.startTime = Date.parse(new Date(val[0])) / 1000;
      this.addExamForm.endTime = Date.parse(new Date(val[1])) / 1000;
    },
    onUpdateExamFormTimeChange(val){
      this.updateExamForm.startTime = Date.parse(new Date(val[0])) / 1000;
      this.updateExamForm.endTime = Date.parse(new Date(val[1])) / 1000;
    },
    /** 提交新增*/
    submitAddExamForm(){
      if(!this.checkSubmitData(this.addExamForm ,this.addExamFormRules)){
        return;
      }
      addExam(this.addExamForm).then(resp => {
          this.getList();
          this.addExamView = false;

      });
    },
    submitUpdateExamForm(){
      if(!this.checkSubmitData(this.updateExamForm ,this.addExamFormRules)){
        return;
      }
      updateExam(this.updateExamForm).then(resp => {
        this.getList();
        this.updateExamView = false;
      });
    },
    checkSubmitData(data ,rules){
      for(let key in rules){
        if(rules[key][0] && rules[key][0].required){
          if(!data[key] || (isArray(data[key]) && data[key].length == 0)){
            this.$modal.msgError(rules[key][0].message);
            return false;
          }
        }
      }
      return true;
    },
  },
};
</script>
